package Demo01;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;


// 练习 Thread 实现多线程下载图片
public class Thread02 extends Thread{
    private String url;  // 网络图片地址
    private String name; // 保存的图片名字

    public Thread02(String url,String name){
        this.url = url;
        this.name = name;
    }

    // 线程下载图片的方法
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);

        System.out.println("下载了文件名为：" + name);
    }

    public static void main(String[] args) {
        Thread02 t1 = new Thread02("http://wx3.sinaimg.cn/large/00869HDlly1gky8a0hgt7j31hc0u04qp.jpg","1.jpg");
        Thread02 t2= new Thread02("http://wx4.sinaimg.cn/large/00869HDlly1gky85yncr6j30u00u0hdu.jpg","2.jpg");
        Thread02 t3 = new Thread02("http://wx2.sinaimg.cn/large/00869HDlly1gky7cczh9fj30u00vxgx6.jpg","3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }

}

class WebDownloader{
    // 下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO 异常，downloader 方法出现问题");
        }
    }
}